<?php

namespace App\Http\Controllers\OpenShop\Merchant;

use App\Http\Controllers\BaseController;
use App\Http\Middleware\OpenShop\MerchantAppAuth;
use App\Service\ChatGroup\ChatGroupMessageReadService;
use App\Service\System\SystemNoticeService;
use App\Service\System\SystemNotifyService;
use GuzzleHttp\Exception\GuzzleException;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class SystemNotifyController extends BaseController
{
    //系统消息中心
    public function popInfo(Request $request)
    {
        $shopId       = (int)$request->header('shopId');
        $presenceArea = $request->get('presenceArea', 1);
        $userId       = MerchantAppAuth::$userId;
        $info         = SystemNotifyService::getUserPopListFirst($userId, $presenceArea);

        return $this->success(['info' => $info]);
    }

    /**
     * ⚠️ 已废弃
     * @param Request $request
     * @return JsonResponse
     */
    //系统消息中心
    public function popList(Request $request)
    {
        $shopId = (int)$request->header('shopId');
        $userId = MerchantAppAuth::$userId;
        $list   = SystemNotifyService::getUserPopList($userId);

        return $this->success(['list' => $list]);
    }

    //系统一级消息中心
    public function firstList(Request $request)
    {
        $shopId = (int)$request->header('shopId');
        $userId = MerchantAppAuth::$userId;
        $list   = SystemNotifyService::getUserNotifyList($userId, $shopId);
        return $this->success(['list' => $list]);
    }

    //系统二级消息中心
    public function secondlist(Request $request)
    {
        $userId   = MerchantAppAuth::$userId;
        $shopId   = (int)$request->header('shopId');
        $pageNum  = (int)$request->get('pageNum', 1);
        $pageSize = (int)$request->get('pageSize', 20);
        $type     = $request->get('type', 1);
        $list     = SystemNotifyService::getUserNotifyListByType($pageSize, $pageNum, $userId, $type);
        return $this->success($list);
    }

    public function info(Request $request)
    {
        $id     = $request->get('id');
        $type   = $request->get('type', 1);
        $userId = MerchantAppAuth::$userId;
        $info   = SystemNotifyService::getNotifyInfoById($userId, $id);

        if (empty($info) || empty($info['notifyCenterId'])) {
            return $this->fail(0, '消息已读或不存在');
        }

        // 已读消息
        SystemNotifyService::readNotify($userId, $info['notifyCenterId']);

        return $this->success(['info' => $info]);
    }

    /**
     * 已读一条消息
     *
     * @param Request $request
     * @return JsonResponse
     */
    public function read(Request $request)
    {
        $notifyCenterId = $request->get('notifyCenterId', 0);
        $notifyUserId   = $request->get('notifyUserId', 0);
        $isOffPop       = $request->get('isOffPop', 0);
        $shopId         = (int)$request->header('shopId');
        $userId         = MerchantAppAuth::$userId;

        // 是否关闭弹窗
        if ($isOffPop) {
            SystemNotifyService::removePopListKey($userId, $notifyCenterId);
        } else {
            SystemNotifyService::readNotify($userId, $notifyCenterId, $notifyUserId);
        }

        return $this->success();
    }

    /**
     * 已读所有消息
     *
     * @param Request $request
     * @return JsonResponse
     */
    public function readAll(Request $request)
    {
        $id     = $request->get('id');
        $shopId = (int)$request->header('shopId');
        $userId = MerchantAppAuth::$userId;

        SystemNotifyService::readAllNotify($userId);

        (new ChatGroupMessageReadService($userId))->readMessageByShopId($shopId);

        return $this->success();
    }

    /**
     * 获取未读数量 & 最近7天消息
     *
     * @param Request $request
     * @return JsonResponse
     */
    public function getUnreadNum(Request $request): JsonResponse
    {
        $userId = MerchantAppAuth::$userId;
        $shopId = (int)$request->header('shopId');
        $list   = SystemNotifyService::getUnreadNum($userId, $shopId);
        return $this->success($list);
    }

    /**
     * 获取消息中心任务看板
     *
     * @param Request $request
     * @return JsonResponse
     * @throws GuzzleException
     */
    public function getTaskBoard(Request $request): JsonResponse
    {
        $list   = SystemNotifyService::getTaskBoard(
            MerchantAppAuth::$userId,
            (int)$request->header('shopId')
        );
        return $this->success($list);
    }

}
